package wrapper;

/**
 * java有8个基本类型，他们是以"值"的形式存在，而非对象
 * 因此8个基本类型不具备面向对象的特性，对此java提供了8个包装类
 * 目的是将基本类型以对象形式
 *
 * 基本类型             包装类
 * byte             java.lang.Byte
 * short            java.lang.Short
 * int              java.lang.Integer
 * long             java.lang.Long
 * float            java.lang.Float
 * double           java.lang.Double
 * char             java.lang.Character
 * boolean          java.lang.Boolean
 */
public class IntegerDemo1 {
    public static void main(String[] args) {
        int a = 123;//-128--127之间，那么valueOf方法会重用对象
//        Integer i1 = new Integer(a);
//        Integer i2 = new Integer(a);
        Integer i1 = Integer.valueOf(a);
        Integer i2 = Integer.valueOf(a);

        System.out.println(i1==i2);//false(new则产生新对象,valueOf则重用对象为true)
        System.out.println(i1.equals(i2));//true

        //Double的valueOf就没有任何多余效果，就是new，但是我们依然建议使用valueOf(不用记那些包装类有额外功能)
        double d = 123.456;
        Double d1 = Double.valueOf(d);
        Double d2 = Double.valueOf(d);
        System.out.println(d1==d2);
        System.out.println(d1.equals(d2));

        System.out.println("--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------");
        //从包装类转换为基本类型
        int i = i1.intValue();
        System.out.println(i);
        double dou = i1.doubleValue();
        System.out.println(dou);
        byte b = i1.byteValue();//注意，大类型转小类型仍然存在溢出问题
        System.out.println(b);

        i = d1.intValue();//123.456
        System.out.println(i);//123,浮点数转换为整数仍然存在丢失精度
        dou = d1.doubleValue();
        System.out.println(dou);//123.456
    }
}





